package com.ruoyi.gateway.config;

import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Description: new java files header..
 * 网关处理接口实现全局异常
 * @author 兰剑
 * @version 1.0
 * @date 2023/3/5 18:31
 */
@Component
public class GlobalException implements HandlerExceptionResolver {


    @Override
    public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object o, Exception e) {
        System.out.printf("参数o：%s", o);
        ModelAndView mv = new ModelAndView();
        if (e instanceof RuntimeException) {
            RuntimeException re = (RuntimeException) e;
            mv.setViewName("error");
            //获取controller层所抛出的异常信息
            mv.addObject("msg", re.getMessage());
        }
        return null;
    }
}
